package com.yoomiito.app.present.account;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.model.bean.InvitePeopleInfoBean;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.base.BaseAccountPresent;
import com.yoomiito.app.ui.account.InvitationCodeActivity;
import com.yoomiito.app.utils.NetworkUtils;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class InvitationCodePresent extends BaseAccountPresent<InvitationCodeActivity> {

    public InvitationCodePresent(AppConfig appConfig) {
        super(appConfig);
    }

    //获取邀请人信息
    public void requestInvitePeopleInfo(String invitationCode){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService().getInviteInfo(invitationCode)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<InvitePeopleInfoBean>getApiTransformerData())
                .compose(XApi.<InvitePeopleInfoBean>getScheduler())
                .subscribe(new ApiSubscriber<InvitePeopleInfoBean>(handleException()) {
                    @Override
                    public void onSuccess(InvitePeopleInfoBean infoBean) {
                        getV().success(infoBean);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }

    //绑定邀请人
    public void requestBindInvitePerson(String mobile,String invitationCode){
        if ( NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService().bingInviteCode(mobile,invitationCode)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.<UserInfo>getScheduler())
                .subscribe(new ApiSubscriber<UserInfo>(handleException()) {
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        getV().bindSuccess(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }
}
